package pers.richard.ormybatis.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;

public class ConvertBeanUtil {
    
    public static <T> List<T> toBeanList(Collection<Object> sourceList, Class<T> clz) {
        if (CollectionUtil.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        List<T> list = new ArrayList<>();
        for (Object source : sourceList) {
            T t = BeanUtil.toBean(source, clz);
            list.add(t);
        }
        return list;
    }
    
    /**
     *	支持不同命名方式的属性之间copy
     */
    public static <T> T beanCopyIgnoreFormat(Object sourceObj, Class<T> destClz) {
        try {
            T destObject = destClz.newInstance();
            List<String> sourceNameList = Stream.of(ClassUtil.getDeclaredFields(sourceObj.getClass())).map(Field::getName).collect(Collectors.toList());
            List<String> destNameList = Stream.of(ClassUtil.getDeclaredFields(destClz)).map(Field::getName).collect(Collectors.toList());
            for (String sourceName : sourceNameList) {
                Object value = BeanUtil.getProperty(sourceObj, sourceName);
                if (ObjectUtil.isEmpty(value)) {
                	continue;
                }
                //源和目标属性名一样set value
                if (ReflectUtil.hasField(destClz, sourceName)) {
                    BeanUtil.setProperty(destObject, sourceName, value);
                    continue;
                }
                //转换成下划线方式的属性名并set value
                String sourceNameLowerUnderscore = CaseFormatUtil.toLowerUnderscore(sourceName);
                if (ReflectUtil.hasField(destClz, sourceNameLowerUnderscore)) {
                    BeanUtil.setProperty(destObject, sourceNameLowerUnderscore, value);
                    continue;
                }
                //转换成驼峰式命名法并set value
                String sourceNameLowerCamel = CaseFormatUtil.toLowerCamel(sourceName);
                if (ReflectUtil.hasField(destClz, sourceNameLowerCamel)) {
                    BeanUtil.setProperty(destObject, sourceNameLowerCamel, value);
                    continue;
                }
                //忽略属性名大小写格式并set value
                String destNameIgnoreCase = destNameList.stream().filter(fieldName -> fieldName.equalsIgnoreCase(sourceName)).findFirst().orElse(null);
                if (destNameIgnoreCase != null) {
                    BeanUtil.setProperty(destObject, destNameIgnoreCase, value);
                }
            }
            return destObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
     
}